package org.zhaya.sdk;

import cn.hutool.core.lang.Assert;
import lombok.NoArgsConstructor;
import org.zhaya.sdk.builder.ConditionsBuilder;
import org.zhaya.sdk.builder.PipelineModelBuilder;
import org.zhaya.sdk.builder.ProcessorNodeBuilder;
import org.zhaya.sdk.model.PipelineDedugResult;
import org.zhaya.sdk.model.PipelineExecuteResult;
import org.zhaya.sdk.model.PipelineNode;
import org.zhaya.sdk.model.ServiceRequestParam;
import org.zhaya.sdk.service.PipelineDebugService;
import org.zhaya.sdk.service.PipelineExecuteService;
import org.zhaya.sdk.service.PipelineParseService;

import java.util.List;
import java.util.Objects;

/**
 * @Author Ryan Yang
 * @Date 2022/9/28 4:13 下午
 */
@NoArgsConstructor
public class PipelineServiceFactory {

    /**
     * 模型构造器
     */
    public static PipelineModelBuilder pipelineModelBuilder() {
        return new PipelineModelBuilder();
    }

    /**
     * 组件节点构造器
     */
    public static ProcessorNodeBuilder processorNodeBuilder() {
        return new ProcessorNodeBuilder();
    }

    /**
     * 条件构造器
     */
    public static ConditionsBuilder conditionsBuilder() {
        return new ConditionsBuilder();
    }

    /**
     * 模型解析器
     */
    public static List<PipelineNode> pipelineModelParse(ServiceRequestParam param) {
        return PipelineParseService.parse(Objects.requireNonNull(param, "request param can not be null").getModelJson());
    }

    /**
     * 模型调试器
     */
    public static List<PipelineDedugResult> pipelineModelDebug(ServiceRequestParam param) {
        Objects.requireNonNull(param, "request param can not be null");
        String debugNodeId = Objects.isNull(param.getDebugNode()) ? param.getDebugNodeId() : param.getDebugNode().getNodeId();
        Assert.notBlank(debugNodeId, "debug node id can not be blank");
        return PipelineDebugService.debug(param.getModelJson(), debugNodeId, param.getRawData());
    }

    /**
     * 模型执行器
     */
    public static PipelineExecuteResult pipelineModelExecute(ServiceRequestParam param) {
        Objects.requireNonNull(param, "request param can not be null");
        return PipelineExecuteService.execute(param.getModelJson(), param.getRawData());
    }
}
